package com.suneee.industry.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suneee.base.util.ContextHelper;
import com.suneee.cloud.admin.dao.AdministratorDao;
import com.suneee.component.excel.ExcelUtil;
import com.suneee.context.page.Page;
import com.suneee.core.error.Assert;
import com.suneee.core.error.ErrorUtils;
import com.suneee.industry.bean.IndustrySearchBean;
import com.suneee.industry.dao.IndustryDao;
import com.suneee.industry.entity.Industry;
import com.suneee.industry.service.IndustryService;
import com.suneee.resources.ProductError;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;
import com.suneee.sso.admin.entity.Administrator;
import com.suneee.web.bean.DownloadInfo;

@Service("api-industry")
public class IndustryServiceImpl implements IndustryService {

	@Autowired
	private IndustryDao industryDao;
	
	@Autowired
	private AdministratorDao administratorDao;
	
	@Autowired
	private ShopDao shopDao;

	@Override
	public void addIndustry(Industry industry) {
		Assert.notNull(industry.getName(), "行业分类名称");
		Assert.notNull(industry.getOrderNumber(), "排序字段");
		if (industryDao.existsName(industry.getName())) {
			throw ErrorUtils.wrap(ProductError.INDUSTRY_NAME_DUPLICATE);
		}
		// 获取当前创建时间
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		industry.setCreateTime(timestamp);

		industry.setCreateUserId(ContextHelper.getCurrentLoginInfo().getAdminId());
		industryDao.insert(industry);
	}

	@Override
	public void updateIndustry(Industry industry) {
		Assert.notNull(industry.getIndustryId(), "行业分类id");
		Assert.notNull(industry.getName(), "行业分类名称");
		Assert.notNull(industry.getOrderNumber(), "排序");
		Industry old = this.getIndustry(industry.getIndustryId());
		if (!old.getName().equals(industry.getName()) && industryDao.existsName(industry.getName())) {
			throw ErrorUtils.wrap(ProductError.INDUSTRY_NAME_DUPLICATE);
		}
		industryDao.updateIndustry(industry);
	}

	@Override
	public void deleteIndustry(Integer... industryId) {
		Assert.notNull(industryId, "行业分类id");
		List<Industry> unDeletes = null;
		try {
			List<Integer> idList = Arrays.asList(industryId);
			List<Industry> list = industryDao.select(idList);
			
			for (Industry industry : list) {
				List<Shop> temp = shopDao.selectWhere(" industry_id = ? ", industry.getIndustryId());
				if(temp != null && !temp.isEmpty()){
					if(unDeletes == null)
						unDeletes = new ArrayList<>();
					unDeletes.add(industry);
				}
			}
			if(unDeletes != null && !unDeletes.isEmpty()){
				list.removeAll(unDeletes);
			}
			if(!list.isEmpty()){
				idList = new ArrayList<>();
				for(Industry industry : list)
					idList.add(industry.getIndustryId());
				industryDao.delete(idList);
			}
		} catch (Exception e) {
			throw ErrorUtils.wrap(ProductError.INDUSTRY_CANNOT_DELETE);
		}
		if(unDeletes != null){
			StringBuffer sbf = new StringBuffer();
			sbf.append("行业分类：");
			for(int i = 0;i < unDeletes.size();i++){
				Industry temp = unDeletes.get(i);
				sbf.append(temp.getName());
				if(i != unDeletes.size() - 1)
					sbf.append("、");
			}
			throw ErrorUtils.wrap(ProductError.INDUSTRY_CANNOT_DELETE,sbf.toString() + ",已经被使用不能删除!");
		}
	}

	@Override
	public Page<Industry> findIndustry(IndustrySearchBean bean) {
		return industryDao.findIndustry(bean);
	}

	@Override
	public Industry getIndustry(int industryId) {
		Assert.notNull(industryId, "行业分类");
		if (industryDao.existsId(industryId)) {
			return industryDao.select(industryId);
		} else {
			throw ErrorUtils.wrap(ProductError.INDUSTRY_DUPLICATE);
		}
	}

	public static final String EXPORT_XLS = "/templates/industry.export.xls";

	/**
	 * 导出
	 */
	@Override
	public DownloadInfo exportIndustrys(Integer[] ids,String name) {
		List<Industry> list = new ArrayList<>();
		if (ids == null) {
			list = this.listIndustry(name);
		} else {
			List<Integer> idList = Arrays.asList(ids);
			list = industryDao.select(idList);
		}
		for(int i = 0; i < list.size(); i++){
			Industry industry = list.get(i);
			int createUserId = industry.getCreateUserId();
			Administrator administrator = administratorDao.select(createUserId);
			if(null != administrator){
				industry.setCreateUserName(administrator.getAccount());
			}
		}
		DownloadInfo downloadInfo = ExcelUtil.writeExcle(EXPORT_XLS, list, ExcelUtil.replaceTimestamp("行业"));

		return downloadInfo;
	}

	@Override
	public List<Industry> listIndustry(String name) {

		return industryDao.listIndustry(name);
	}

	@Override
	public List<Industry> findIndustryList(IndustrySearchBean name) {
		return industryDao.select();
	}

}
